from tools.utils import Mouse
import keyboard
# import numpy as np
# from tools.interact_recoder_rel_move import run_warm_up
from bdtime import tt
from tools.interact_recoder_rel_move import screen_width, screen_height


ms = Mouse()


class MouseEvents:
    dx = dy = 50
    # dx = screen_width // 4
    # dy = screen_height // 4

    shift_speed = 0.3

    def click_left(self):
        ms.click(ms.buttons.left)

    def click_right(self):
        ms.click(ms.buttons.right)

    def click_middle(self):
        ms.click(ms.buttons.middle)

    def move_left_small(self):
        ms.move_r(-self.dx, 0)

    def move_right_small(self):
        ms.move_r(self.dx, 0)

    def move_left(self):
        ms.move_r(-self.dx, 0)

    def move_right(self):
        ms.move_r(self.dx, 0)

    def move_up(self):
        ms.move_r(0, -self.dy)

    def move_down(self):
        ms.move_r(0, self.dy)

    def move_left_shift_speed(self):
        ms.move_r(int(-self.dx * self.shift_speed), 0)

    def move_right_shift_speed(self):
        ms.move_r(int(self.dx * self.shift_speed), 0)

    def move_up_shift_speed(self):
        ms.move_r(0, int(-self.dy * self.shift_speed))

    def move_down_shift_speed(self):
        ms.move_r(0, int(self.dy * self.shift_speed))


class KeyMoveMouse:
    click_left = '0'
    click_middle = '5'
    click_right = '4'

    move_left = 'left'
    move_right = 'right'
    move_up = 'up'
    move_down = 'down'

    _shift_speed_key = '.'
    move_left_shift_speed = f'{_shift_speed_key} + left'
    move_right_shift_speed = f'{_shift_speed_key} + right'
    move_up_shift_speed = f'{_shift_speed_key} + up'
    move_down_shift_speed = f'{_shift_speed_key} + down'


key_move_mouse = KeyMoveMouse()
mouse_events = MouseEvents()


def main():
    hot_event_names = [i for i in key_move_mouse.__dir__() if not i.startswith('_')]
    hot_keys = [getattr(key_move_mouse, i) for i in hot_event_names]
    hot_funcs = [getattr(mouse_events, i) for i in hot_event_names]

    while True:
        # Wait for the next event.
        event = keyboard.read_event()
        if event.event_type == keyboard.KEY_DOWN and event.name in hot_keys:
            key_name = event.name
            if keyboard.is_pressed(key_move_mouse._shift_speed_key):
                if key_name in ['left', 'right', 'up', 'down']:
                    key_name = f'{key_move_mouse._shift_speed_key} + {key_name}'
            index = hot_keys.index(key_name)
            event_verbose_name = hot_event_names[index]
            func = hot_funcs[index]

            print('--- key_name:', key_name, ', event_verbose_name:', event_verbose_name)

            func()


if __name__ == '__main__':
    main()

    # keyboard.add_hotkey(key_move_mouse.move_left, mouse_events.move_left)
    # keyboard.add_hotkey(key_move_mouse.move_right, mouse_events.move_right)
    # keyboard.add_hotkey(key_move_mouse.move_up, mouse_events.move_up)
    # keyboard.add_hotkey(key_move_mouse.move_down, mouse_events.move_down)
    #
    # keyboard.add_hotkey(key_move_mouse.move_left_shift_speed, mouse_events.move_left_shift_speed)
    # keyboard.add_hotkey(key_move_mouse.move_right_shift_speed, mouse_events.move_right_shift_speed)
    # keyboard.add_hotkey(key_move_mouse.move_up_shift_speed, mouse_events.move_up_shift_speed)
    # keyboard.add_hotkey(key_move_mouse.move_down_shift_speed, mouse_events.move_down_shift_speed)
    #
    # keyboard.add_hotkey(key_move_mouse.click_left, mouse_events.click_left)
    # keyboard.add_hotkey(key_move_mouse.click_middle, mouse_events.click_middle)
    # keyboard.add_hotkey(key_move_mouse.click_right, mouse_events.click_right)
    #
    # keyboard.wait('esc')

    # while True:  # 这里用来获取各个key的name
    #   keyboard_event = keyboard.read_event(True)
    #   print(keyboard_event.event_type, '---', keyboard_event.name)

    # key__mouse_move_left = '1'
    # keyboard.key_to_scan_codes('')


